
package GameLogic;

import java.util.*;
import org.lwjgl.util.*;
import Enums.*;
import Settings.*;

/**
 *
 * @author RikuXan + Mastre
 */
public class Game
{
    //Spielkarte
    private Map map;
    //Liste der Spieler
    private Vector<Player> players;
    //Liste der temporären Elemente
    private Temporaries temps;


    public Game()
    {
        map = new Map();
        players = new Vector<Player>();
        temps = new Temporaries();
        //lastmove = new Vector<_Long>();
    }

    //player-Methoden
    //geht die Liste der Spieler durch und leitet den Befehl zum bewegen der Schlange weiter
    public void move()
    {
        for( int i = 0; i < players.size(); i++ )
        {
            players.elementAt(i).move();
            players.elementAt(i).setScore(players.elementAt(i).getScore()+Settings.scorePerMove);
        }
    }

    public void checkPlayerSize()
    {
        for( int i = 0 ; i < players.size() ; i++ )
            players.elementAt(i).checkSize();
    }

    public void resetPlayerScore(int playerNr)
    {
        players.elementAt(playerNr).setScore(0);
    }

    public int returnPlayerScore( int playerNr )
    {
        return players.elementAt(playerNr).getScore();
    }

    //überprüft ob der Spieler mit der Nummer playerNr in diesem Zug stirbt
    public boolean playerIsDead( int playerNr )
    {
        Player curPlayer = players.elementAt( playerNr );
        int headPosX = curPlayer.getElementPosX(0);
        int headPosY = curPlayer.getElementPosY(0);
        boolean dead = false;
        for ( int i = 0 ; i < players.size() ; i ++)
        {
            for ( int j = 0  ; j < players.elementAt(i).getSnakeElementCount() ; j++)
            {
                if ( !( i == playerNr && j <= 0 ) )
                    if( ( headPosX == players.elementAt(i).getElementPosX(j) ) &&  ( headPosY == players.elementAt(i).getElementPosY(j) ) )
                        dead = true;
            }
        }
        if( ( map.returnEffects( headPosX , headPosY ) & Effects.DEADLY.code ) == Effects.DEADLY.code)
            dead = true;
        return dead;
    }

    public void resetSnake( int player )
    {
        players.elementAt(player).resetSnake();
    }

    public void setSnakeLength( int player , int length )
    {
        players.elementAt( player ).setSnakeLength( length );
    }

    public void setSnakeDirection( Direction d )
    {
        players.firstElement().setSnakeOrientation(d);
    }

    public Direction getSnakeOrientation()
    {
        return players.firstElement().getSnakeOrientation();
    }

    public int returnPlayerCount()
    {
        return players.size();
    }

    public void setPlayerHead( int player , int x , int y )
    {
        players.elementAt( player ).setHead( x , y );
    }

    public void addPlayer()
    {
        players.addElement( new Player() );
    }

    public int snakePosX( int playerNr , int position )
    {
        return players.elementAt( playerNr ).getElementPosX( position );
    }

    public int snakePosY( int playerNr , int position )
    {
        return players.elementAt( playerNr ).getElementPosY( position );
    }

    public String snakeReturnTexture( int playerNr , int position )
    {
        return players.elementAt( playerNr ).getTexture( position );
    }

    public boolean playerAt(int position)
    {
        if( players.size() - 1 >=  position )
            return true;
        else
            return false;
    }

    public int getSnakeLength(int playerNr)
    {
        return players.elementAt(playerNr).getSnakeElementCount();
    }

    public boolean ElementInstanceOf( int player , int pos , Class c )
    {
        return players.elementAt(player).ElementInstanceof( pos , c );
    }

    public int getScore(int playerNr)
    {
        return players.elementAt(playerNr).getScore();
    }

    //map-Methoden
    public String mapReturnTexture(int x , int y)
    {
        return map.returnTexture(x, y);
    }

    public Dimension returnSpawnAt( int i )
    {
        return map.returnSpawnAt( i );
    }

    public void loadMap( String mapname )
    {
        map.loadMap( mapname );
    }

    public int mapHeight()
    {
        return map.returnHeight();
    }

    public int mapWidth()
    {
        return map.returnWidth();
    }

    public boolean mapIsFoodable(int x , int y)
    {
        return map.isFoodable(x,y);
    }

    //temps-Methoden
    public int getTempsLength()
    {
        return temps.getLength();
    }

    public boolean tempsHasFood()
    {
        return temps.hasFood();
    }

    public boolean tempIsFood(int nr)
    {
        return temps.isFood(nr);
    }

    public int tempPosX(int nr)
    {
        return temps.getPosX(nr);
    }

    public int tempPosY(int nr)
    {
        return temps.getPosY(nr);
    }

    public void snakeEat(int nr)
    {
        players.elementAt(nr).eat();
        players.elementAt(nr).setScore(players.elementAt(nr).getScore() + Settings.scorePerFood);
    }

    public void removeTemp(int nr)
    {
        temps.removeElement(nr);
    }

    public String tempReturnTexture(int pos)
    {
        return temps.returnTexture( pos );
    }

    public void generateFood()
    {
        int x = 0;
        int y = 0;
        Random random = new Random();
        boolean infoodable = false;
        while( !tempsHasFood() )
        {
            infoodable = false;
            while( !mapIsFoodable( x , y ) || infoodable)
            {
                x = random.nextInt( mapWidth() - 1 );
                y = random.nextInt( mapHeight() - 1 );
                infoodable = false;
                for ( int i = 0 ; i < players.size() ; i ++)
                {
                    for ( int j = 0  ; j < players.elementAt(i).getSnakeElementCount() ; j++)
                    {
                            if( ( x == players.elementAt(i).getElementPosX(j) ) &&  ( y == players.elementAt(i).getElementPosY(j) ) )
                                infoodable = true;
                    }
                }
            }
        temps.generateFood( x , y );
        }
    }

    public void clearTemps()
    {
        temps.clear();
    }

}
